package web;

import application.MySession;
import application.authorization.MyRoles;
import org.apache.wicket.Component;
import org.apache.wicket.IPageMap;
import org.apache.wicket.PageParameters;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import web.admin.AdminPage;
import web.commons.SecureLink;
import web.eshop.panels.ShoppingCartPanel;
import web.login.LoginPanel;

import java.util.HashMap;
import java.util.Map;


public abstract class BasePage extends WebPage {
    @SpringBean
    private MySession mySession;

    public abstract IModel getPageTitle();

    public abstract IModel getDescription();

    public abstract IModel getKeywords();

    protected abstract Component newLeftPanel(String id);

    protected abstract Component newRightPanel(String id);

    protected abstract Component newTopPanel(String id);

    protected Component newUriPanel(String id) {
        return new EmptyPanel(id);
    }

    String query;

    protected BasePage() {
        StatelessForm form = new StatelessForm("searchForm", new CompoundPropertyModel(this)) {

            @Override
            protected void onSubmit() {
                Map<String, String> map = new HashMap<String, String>();
                map.put("0", query);
                PageParameters pageParameters = new PageParameters(map);
                setResponsePage(SearchPageResult.class, pageParameters);

            }
        };
        TextField t = new TextField("query");
        t.setRequired(true);
        form.add(t);
        add(form);

        add(new BookmarkablePageLink("map", MapPage.class));
        add(new SecureLink("adminPage", AdminPage.class, RENDER, MyRoles.ADMIN));

    }

    @Override
    protected void onBeforeRender() {
        // add the <title> tag
        addOrReplace(new Label("title", getPageTitle()));

        Label desc = new Label("description", "");
        desc.add(new AttributeAppender("content", getDescription(), " "));
        addOrReplace(desc);

        Label keywords = new Label("keywords", "");
        keywords.add(new AttributeAppender("content", getKeywords(), " "));
        addOrReplace(keywords);
        if (get("top") == null) {
            // subclass-driven components not yet initilized
            addOrReplace(newTopPanel("top"));
        }
        if (get("left") == null) {
            // subclass-driven components not yet initilized
            addOrReplace(newLeftPanel("left"));
        }
        if (get("uri") == null) {
            // subclass-driven components not yet initilized
            addOrReplace(newUriPanel("uri").setVisible(false));
        }

        if (get("right") == null) {
            // subclass-driven components not yet initilized
            addOrReplace(newRightPanel("right"));
        }
        if (get("feedback") == null) {
            // subclass-driven components not yet initilized
            FeedbackPanel panel = new FeedbackPanel("feedback");
            panel.setOutputMarkupId(true);
            addOrReplace(panel);
        }
        if (get("loginOrShoppingCart") == null) {
            // subclass-driven components not yet initilized
            if (AuthenticatedWebSession.get().isSignedIn()) {
                addOrReplace(new ShoppingCartPanel("loginOrShoppingCart"));
            } else {
                addOrReplace(new LoginPanel("loginOrShoppingCart"));
            }
        }
        super.onBeforeRender();
    }

    protected BasePage(IModel<?> model) {
        this();
    }

    protected BasePage(IPageMap pageMap) {
        this();
    }

    protected BasePage(IPageMap pageMap, IModel<?> model) {
        this();
    }

    protected BasePage(IPageMap pageMap, PageParameters parameters) {
        this();
    }

    protected BasePage(PageParameters parameters) {
        this();
    }

}
